package com.educhains.web.ctrl.backend;


import com.alibaba.fastjson.JSONObject;
import com.educhains.module.demo.vo.Page;
import com.educhains.module.post.service.PostReplyService;
import com.educhains.module.post.service.PostService;
import com.educhains.module.post.vo.Post;
import com.educhains.module.post.vo.PostContent;
import com.educhains.web.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
public class PostMaintain {

    private static final String  TYPE_PATH = "/WEB-INF/jsp/backend/maintain/panelList/postList.jsp";
    private static final String  TYPE = "post";

    @Autowired
    private PostService postService;
    @Autowired
    private PostReplyService postReplyService;


    @RequestMapping(value = "/maintain/")
    public String maintainPage(HttpServletRequest request) {

        return "backend/maintain/mainPage";
    }

    @RequestMapping(value = "/maintain/postManage")
    public String postManage(HttpServletRequest request) {


        String titleFragment = request.getParameter("title_fragment");
        String pageNum = request.getParameter("p") == null ? "1" : request.getParameter("p");

        Page postPage;
        if(titleFragment == null || titleFragment.equals("") || titleFragment.equals("undefined")) {
            postPage = postService.showPostPage(Integer.valueOf(pageNum), 1);
        } else {
            postPage = postService.searchByTitlePage(Integer.valueOf(pageNum),titleFragment);
        }

        request.setAttribute("type", TYPE);
        request.setAttribute("typePath", TYPE_PATH);
        request.setAttribute("postPage", postPage);
        request.setAttribute("titleFragment", titleFragment);

        return "backend/maintain/operationPanel";
    }



    @ResponseBody
    @RequestMapping(value = "/maintain/postModify")
    public String postModify(HttpServletRequest request) {

//        String nameContent = request.getParameter("name_content"); //获取前台用户昵称，修改功能待定
//        int classification = Integer.parseInt(request.getParameter("classification"));
        String postId = request.getParameter("post_id");
        String postTitle = request.getParameter("post_title");
//        String postDate = request.getParameter("post_date");    //发帖时间不建议更改
        String postAuthor = request.getParameter("post_author");
        String postContentText = request.getParameter("post_content");
        String isEssential = request.getParameter("is_essential");

        Post post = postService.selectPostByPrimaryKey(postId);
        PostContent postContent = postService.selectPostContentByPrimaryKey(postId);


        if(!postTitle.equals(post.getPostName())) {
            post.setPostName(postTitle);
        }
        if(!postAuthor.equals(post.getPostPubName())) {
            post.setPostPubName(postAuthor);
        }
        //未选中的时候返回 null
        if("true".equals(isEssential)) {
            post.setPostIsEssence(true);
        }else {
            post.setPostIsEssence(false);
        }

        postContent.setPostText(postContentText);

        postService.updatePostByPrimaryKeySelective(post);
        postService.updatePostContentByPrimaryKeySelective(postContent);


        return returnBody(true, "modify successfully");
    }

    @ResponseBody
    @RequestMapping(value = "/maintain/postDelete")
    public String postDelete(HttpServletRequest request) {

        String postId = request.getParameter("post_id");

        postService.deletePostByPrimaryKey(postId);

        return returnBody(true, "delete successfully");
    }

    private static String returnBody(boolean success, String msg) {
        JSONObject json = new JSONObject();
        if (success) {
            json.put("success", true);
            json.put("msg", msg);
        } else {
            json.put("success", false);
            json.put("msg", msg);
        }
        //return json.toJSONString();
        return JsonUtils.toJSONString(json);
    }
}
